#include "page4_widget/page4_widget.h"
#include "user/my_protocol.h"
#include "modbus_master.h"

#define SLAVE_ID    1
Page4_Widget::Page4_Widget(WelcomeWidget *widget)
{
    this->welcomewidget = widget;
    this->welcome_ui = this->welcomewidget->ui;

    mb_master = new Modbus_Master(SLAVE_ID);

    if(mb_master != nullptr)
    {
        connect(this,SIGNAL(signal_connect_master(QString, int)),mb_master,SLOT(connect_modbus_master(QString, int)));
        connect(this,SIGNAL(signal_disconnect_master()),mb_master,SLOT(disconnect_modbus_master()));
        connect(mb_master,SIGNAL(signal_send_slave_info(QString)),this,SLOT(slot_display_slave_info(QString)));
    }
    connect(welcome_ui->pb_start_poll,SIGNAL(clicked()),this,SLOT(on_pb_start_poll_clicked()));     //建立信号槽
    connect(welcome_ui->pb_clear_rx,SIGNAL(clicked()),this,SLOT(on_pb_clear_rx_clicked()));     //建立信号槽

    welcome_ui->pb_start_poll->setText("开 始");
    welcome_ui->pb_start_poll->setCheckable(false);

}

Page4_Widget::~Page4_Widget()
{
    this->welcomewidget = nullptr;
    this->welcome_ui = nullptr;

}

void Page4_Widget::slot_display_slave_info(QString str_display)
{
    welcome_ui->plainTextEdit_uart_rx->appendPlainText(str_display);
}


void Page4_Widget::on_pb_start_poll_clicked()
{
    //Reg_Dialog *reg_dialog = new Reg_Dialog();

    //reg_dialog->exec();

    //delete reg_dialog;


    if(welcome_ui->pb_start_poll->isCheckable())
    {
        emit signal_disconnect_master();

        welcome_ui->pb_start_poll->setText("开 始");
        welcome_ui->pb_start_poll->setCheckable(false);
    }
    else
    {
        QString port_name = QString(RS485_SERIAL_PORT);
        int modbus_baud = 115200;

        emit signal_connect_master(port_name,modbus_baud);

        welcome_ui->pb_start_poll->setText("停 止");
        welcome_ui->pb_start_poll->setCheckable(true);
    }

}


void Page4_Widget::on_pb_clear_rx_clicked()
{
    welcome_ui->plainTextEdit_uart_rx->clear();
    welcome_ui->plainTextEdit_cardnum->clear();
    welcome_ui->plainTextEdit_doornum->clear();
    welcome_ui->plainTextEdit_state->clear();
}


